<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <TreeToole :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <!-- 静态树型这里用了一个行列布局 -->
          <!--
               传入内容 插槽内哦那个 会循环多次，有多少节点就循环多少次
               作用域插槽 slot-scoped='obj' 接收传递给插槽得数据 data是每个节点的数据对象
               这里的data是 el-tree里面的:data属性
           -->
          <!-- 当子组件点击删除按钮删除成功后触发自定义事件@delDepts="" 然后重新调用getDepartments获取数据 -->
          <TreeToole slot-scope="{ data }" :tree-node="data" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts" />
        </el-tree>
      </el-card>
    </div>
    <Adddept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
  </div>
</template>

<script>
import TreeToole from './component/tree-toole.vue'
import Adddept from '@/views/departments/component/add-dept.vue'
import { getDepartments } from '@/api/departments'
// 引入树型架构递归工具
import { tranListToTreeData } from '@/utils/index'
export default {
  components: {
    TreeToole,
    Adddept
  },
  data() {
    return {
      departs: [
        // { name: '总裁办', manager: '曹操', children: [{ name: '董事会', manager: '曹丕' }] },
        // { name: '行政部', manager: '刘备' },
        // { name: '人事部', manager: '孙权' }
      ],
      company: { },
      defaultProps: {
        label: 'name' // 表示从这个属性中显示内容
      },
      showDialog: false,
      node: null
    }
  },
  created() {
    this.getDepartments() // 钩子函数 页面初始化前调用
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments() // 获取组织架构数据
      console.log(result)
      this.company = { name: result.companyName, manager: '负责人', id: '' } // id?
      this.departs = tranListToTreeData(result.depts, '')
    },
    addDepts(node) {
      // node 是子组件传递过来的第二个参数值
      this.showDialog = true // 显示弹窗
      this.node = node // 因为node是当前点击的部门，所以此时这个部门应该被记录下来
    },
    editDepts(node) {
      this.showDialog = true // 显示弹窗
      this.node = node // 是当前点击的部门，所以此时这个部门应该被记录下来
      //   console.log(this.$refs.addDept)
      this.$refs.addDept.getDepartDetail(node.id)
    }
  }
}
</script>

<style scoped>
 .tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
